<?php

namespace App\CompanyApi\Services\Auth;

use App\Exceptions\ApiExceptions;
use App\Models\CompanyLoginModel;
use App\Models\CompanyModel;
use Illuminate\Contracts\Auth\Authenticatable;

class AuthService
{
    /**
     * 登陆方法
     * @param string $mobile
     * @param string $password
     * @return array
     * @throws ApiExceptions
     */
    public function login(string $mobile, string $password)
    {
        $credentials = [
            'mobile' => $mobile,
            'password' => $password,
        ];

        if (!$token = auth('company-api')->attempt($credentials)) {
            throw new ApiExceptions('账号或密码错误');
        }
        $expiresIn = auth('company-api')->factory()->getTTL() * 60;
        $userInfo = auth('company-api')->user();
        $userInfo->last_at = now();
        $userInfo->last_ip = request()->getClientIp();
        $userInfo->save();
        return [
            'access_token' => $token,
            'username' => $userInfo->contact_person_name,
            'token_type' => 'Bearer',
            'expires_in' => $expiresIn
        ];
    }
    /**
     * 获取用户信息
     * @return array
     */
    public function me(CompanyLoginModel|Authenticatable $users)
    {
        //取出用户信息
        return $users->only('company_name', 'business_license_photo', 'company_logo','identity_card_front',
            'identity_card_back','contact_person_avatar','contact_person_name','mobile','is_lock','cur_type');
    }


}
